#include <memory>
#include "calServer.hpp"

using namespace server;
static void Usage(std::string proc)
{
    std::cout << "Usage:\n\t" << proc << "serverPort\n\n";
}
// 根据传入的req，输出resp
bool cal(const Request &req, Response &resp)
{
    resp._exitCode = OK;
    resp._result = OK;
    int x = req._x, y = req._y;
    switch (req._op)
    {
    case '+':
        resp._result = x + y;
        break;
    case '-':
        resp._result = x - y;
        break;
    case '*':
        resp._result = x * y;
        break;
    case '/':
    {
        if (0 == y)
        {
            resp._exitCode = DIV_ZERO;
        }
        else
            resp._result = x / y;
        break;
    }
    case '%':
    {
        if (0 == req._y)
        {
            resp._exitCode = MOD_ZERO;
        }
        else
            resp._result = req._x % req._y;
        break;
    }
    default: // 不是 + - * / % 非法
        resp._exitCode = OP_ERROR;
        return false;
    }
    std::cout << "cal success" << std::endl;
    return true;
}
//./tcpServer local_port
int main(int argc, char *argv[])
{
    if (argc != 2) // 判断外部传入的参数是否为2
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<CalServer> tsvr(new CalServer(port));
    tsvr->initServer();
    tsvr->start(cal); // 传入回调函数
    return 0;
}
